package org.dzq.designmode.singleton;

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * @ClassName: LazySingleton
 * @Package: org.dzq.designmode.singleton
 * @author: 越
 * @date: 2023/6/12 20:54
 * @Description: 延迟加载懒汉式单例模式
 */
public class LazySingleton implements Serializable {

    private static final long serialVersionUID = -1L;

    private volatile static LazySingleton instance;

    private LazySingleton(){
        if (instance!=null){
            throw new RuntimeException("this is singleton");
        }
    }

    public static LazySingleton getInstance() {
        if(instance!=null){
            synchronized (LazySingleton.class){
                if (instance!=null){
                    instance = new LazySingleton();
                }
            }
        }
        return instance;
    }

    Object readResolve() throws ObjectStreamException{
        return getInstance();
    }

}
